//
//  LYExchangeCoinsViewController.m
//  Btpin
//
//  Created by liuyan on 2019/4/22.
//  Copyright © 2019年 Vic. All rights reserved.
//

#import "LYExchangeCoinsViewController.h"

@interface LYExchangeCoinsViewController ()

@property (nonatomic ,strong) UITextField *coinsTextfiled;
@property (nonatomic ,strong) NSDictionary *dataDic;

@end

@implementation LYExchangeCoinsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestAmountData];
}

- (void)createSubview{
    self.title = @"兑换聘豆";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, WIDTH - 30, 20)];
    [self.view addSubview: titleLabel];
    titleLabel.textColor = RGB(34, 34, 34);
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.text = @"账户余额";
    
    UILabel *blanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.bottom + 5, WIDTH - 30, 20)];
    [self.view addSubview: blanceLabel];
    blanceLabel.textColor = [UIColor orangeColor];
    blanceLabel.font = [UIFont systemFontOfSize:18];
    CGFloat amount = [self.dataDic[@"amount"] floatValue];
    blanceLabel.text = [UIToolsControl showAmount:[NSString stringWithFormat:@"%.2f",amount]];
    
     UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(15, blanceLabel.bottom + 10,WIDTH - 30 , 1)];
    line1.backgroundColor = RGB(247, 247, 247);
    [self.view addSubview:line1];
    
    UILabel *exchangetitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, line1.bottom+ 10, WIDTH - 30, 20)];
    [self.view addSubview: exchangetitleLabel];
    exchangetitleLabel.textColor = RGB(34, 34, 34);
    exchangetitleLabel.font = [UIFont systemFontOfSize:14];
    exchangetitleLabel.text = @"兑换聘豆";
    
    self.coinsTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(15, exchangetitleLabel.bottom+ 5, WIDTH - 30, 45)];
    self.coinsTextfiled.textColor = RGB(34, 34, 34);
    self.coinsTextfiled.font = [UIFont systemFontOfSize:18];
    self.coinsTextfiled.keyboardType = UIKeyboardTypeNumberPad;
    self.coinsTextfiled.placeholder = @"请输入兑换的聘豆";
    [self.view addSubview:self.coinsTextfiled];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(15, self.coinsTextfiled.bottom + 5,WIDTH - 30 , 1)];
    line2.backgroundColor = RGB(247, 247, 247);
    [self.view addSubview:line2];
    
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, line2.bottom+ 10, WIDTH - 30, 20)];
    [self.view addSubview: messageLabel];
    messageLabel.textColor = [UIColor orangeColor];
    messageLabel.font = [UIFont systemFontOfSize:14];
    messageLabel.adjustsFontSizeToFitWidth = YES;
    messageLabel.text = @"注：10聘豆=1.0元,兑换成功后，相应余额将被扣除";
    
    UIButton *applybutton = [UIButton buttonWithType:UIButtonTypeCustom];
    applybutton.frame = CGRectMake( 40, messageLabel.bottom + 30, WIDTH-80, 40);
    applybutton.layer.cornerRadius = applybutton.height/2;
    applybutton.clipsToBounds = YES;
    applybutton.backgroundColor = [UIColor orangeColor];
    [applybutton setTitle:@"兑 换" forState:UIControlStateNormal];
    [applybutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    applybutton.titleLabel.font = [UIFont systemFontOfSize:15];
    [applybutton addTarget:self action:@selector(applybuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:applybutton];
}

- (void)requestAmountData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Wallet_getChargeConfig_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            [weakSelf organizeData:responseObject[@"data"]];
        }else if ([responseObject[@"code"] integerValue] == 1400){
            SVPHUDError(responseObject[@"msg"]);
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

- (void)organizeData:(NSDictionary *)dic{
    self.dataDic = dic;
    [self createSubview];
}

- (void)applybuttonClick:(UIButton *)button{
    [self.coinsTextfiled resignFirstResponder];
    NSString *coins = self.coinsTextfiled.text;
    if (coins.length == 0) {
        SVPHUDError(@"兑换聘豆不能为空");
        return;
    }
    
    if ([coins integerValue] == 0) {
        SVPHUDError(@"兑换聘豆数必须大于0");
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    dic[@"coins"] = [NSNumber numberWithInteger:[coins integerValue]];
    __weak typeof(self) weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Wallet_exchangeCoins_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(@"兑换成功");
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

@end
